<template>
  <div>
    <el-dialog width="80%" :visible.sync="dialogVisible" @close="onClose" :title="title">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="250px"
               label-position="left">
        <el-form-item  label="合成材料名称" prop="materialName">
          <el-input v-model="formData.materialName" placeholder="合成材料名称"></el-input>
        </el-form-item>
        <el-form-item  label="该材料的合成下一级的概率" prop="materialChance">
          <el-input v-model="formData.materialChance" placeholder="该材料的合成下一级的概率"></el-input>
        </el-form-item>
        <el-form-item  label="该材料的增加合成飞机概率" prop="synthesisChance">
          <el-input v-model="formData.synthesisChance" placeholder="该材料的增加合成飞机概率"></el-input>
        </el-form-item>
        <el-form-item  label="合成材料图片" prop="materialUrl">
          <ImageUpload v-model="formData.materialUrl" :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {syntheticMUpdate, syntheticMADD} from "@/api/aircraft/aircraft"

export default {
  data() {
    return {
      dialogVisible: false,
      title: '编辑',
      formData: {
        materialChance: undefined,
        materialLevel: undefined,
        materialName: undefined,
        materialUrl: undefined,
        outputProbability: undefined,
        synthesisChance: undefined
      },
      rules: {
        materialChance: [{
          required: true,
          message: '请输入该材料的合成下一级的概率',
          trigger: 'blur'
        }],
        materialLevel: [{
          required: true,
          message: '请输入产出该材料所需的层级（正整数）',
          trigger: 'blur'
        }],
        materialName: [{
          required: true,
          message: '请输入合成材料名称',
          trigger: 'blur'
        }],
        materialUrl: [{
          required: true,
          message: '请输入合成材料图片',
          trigger: 'blur'
        }],
        outputProbability: [{
          required: true,
          message: '请输入该材料的在层级产出概率',
          trigger: 'blur'
        }],
        synthesisChance: [{
          required: true,
          message: '请输入该材料的增加合成飞机概率',
          trigger: 'blur'
        }],
      },
      id:null
    }
  },

  methods: {
    // 弹窗
    async onOpen(obj) {
      if(obj){
        this.id = true
        this.title="编辑"
        this.formData = JSON.parse(JSON.stringify(obj))
      }else{
        this.id = false
        this.title="新增"
      }
      this.dialogVisible = true
    },
    onClose() {
      this.dialogVisible = false
      this.$refs['elForm'].resetFields()
      this.formData.materialChance = undefined
      this.formData.materialLevel = undefined
      this.formData.materialName = undefined
      this.formData.materialUrl = undefined
      this.formData.outputProbability = undefined
      this.formData.synthesisChance = undefined
    },
    close() {
      this.dialogVisible = false
    },
    // 提交数据
    handleConfirm() {
      this.$refs['elForm'].validate(async valid => {
        this.loading = true
        if (!valid) {
          // 如果验证不通过，则阻止后续代码执行
          this.loading = false;
          return;
        }
        if (this.id) {
          try {
            // 编辑
            await syntheticMUpdate(this.formData)
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        } else {
          try {
            // 新增
            await syntheticMADD(this.formData)
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        }
      })
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
